<!-- 收银台 -->
<template>
	<view class="pay-method-wrap">
		<u-navbar title="支付方式" :is-back="false" :border-bottom="false" title-color="#111">
			<view class="page_navbar_warp" @click="goBack(1)">
				<image src="../../static/images/outpatient/left.png" mode="" class="navLeft"></image>
			</view>
		</u-navbar>

		<view class="u-flex-col u-col-center money-box">
			<text class="time">倒计时</text>
			<view class="money">{{price}}</view>
		</view>

		<!-- 支付方式单选项 -->
		<u-radio-group class="pay-box" v-model="payType" active-color="#f0c785">
			<!-- 微信支付 -->
			<view class="u-flex u-row-between pay-item" @tap="payType = 'wechat'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_wx_pay.png'" mode=""></image>
					<text>微信支付</text>
				</view>
				<u-radio shape="circle" name="wechat"></u-radio>
			</view>
			<!-- 支付宝支付 -->
			<!-- <view class="u-flex u-row-between pay-item" @tap="payType = 'alipay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_ali_pay.png'" mode=""></image>
					<text>支付宝支付</text>
				</view>
				<u-radio shape="circle" name="alipay"></u-radio>
			</view> -->
			<!-- 苹果支付 -->
			<!-- <view class="u-flex u-row-between pay-item" @tap="payType = 'iospay'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_apple_pay.png'" mode=""></image>
					<text>ApplePay</text>
				</view>
				<u-radio shape="circle" name="iospay"></u-radio>
			</view> -->
			<!-- 余额支付 -->
			<view class="u-flex u-row-between pay-item" @tap="payType = 'wallet'">
				<view class="u-flex">
					<image class="pay-img" :src="$IMG_URL + '/imgs/order/order_wallet_pay.png'" mode=""></image>
					<text>余额支付</text>
				</view>
				<u-radio shape="circle" name="wallet"></u-radio>
			</view>
		</u-radio-group>

		<button class="u-reset-button pay-btn" @tap="confirmPay">确认支付 ￥{{price}}</button>
	</view>
</template>

<script>
	import {
		BASE_URL
	} from '@/env.js'
	export default {
		components: {},
		data() {
			return {
				payType: 'wechat', //支付方式
				name: '',
				price: '',
				mobile: '',
				clinc_activity_id: '',
				baseUrl: this.$baseUrl, //后台域名地址
			};
		},
		onShow() {

		},
		onLoad(option) {
			if (option.name) {
				this.name = decodeURIComponent(option.name);
				console.log(this.name)
				this.mobile = option.mobile;
				this.price = option.price;
				this.clinc_activity_id = option.clinc_activity_id;
			}
		},

		methods: {
			confirmPay() {
				uni.request({
					url: this.baseUrl + "/api/Clinc/activity_baoming",
					data: {
						name: this.name,
						mobile: this.mobile,
						clinc_activity_id: this.clinc_activity_id,
						token: uni.getStorageSync('token')
					},
					success: val => {
						if (val.data.code == 1) {
							uni.request({
								url: this.baseUrl + "/addons/shopro/pay/prepay",
								method:"POST",
								data: {
									payment: this.payType,
									order_sn: val.data.data.order_sn,
									openid: uni.getStorageSync('openid'),
									token: uni.getStorageSync('token'),
									platform:'wxMiniProgram',
								},
								success: res => {
									console.log(55555);
									console.log(res.data);
									console.log(6666);
									console.log(res.data.data);
									if (res.data.code == 1) {
										console.log(res.data.data.pay_data);
										// * 微信、支付宝，支付
										// * payType：支付类型
										// * orderInfo：订单数据
										// * payType == wxpay，则orderInfo是对象，支付宝orderInfo为字符串
										if (this.payType == 'wechat' || this.payType == 'alipay') {
											uni.requestPayment({
												provider: this.payType == 'wechat' ?
													'wxpay' : 'alipay', //支付类型
												orderInfo: res.data.data.pay_data,
												timeStamp: res.data.data.pay_data.timeStamp,
												nonceStr: res.data.data.pay_data.nonceStr,
												package: res.data.data.pay_data.package,
												signType: res.data.data.pay_data.signType,
												paySign: res.data.data.pay_data.paySign,
												success: (res) => {
													console.log(res);
													if (res.errMsg ==
														"requestPayment:fail") {
														uni.showToast({
															icon: 'none',
															mask: true,
															title: '支付失败',
														});
													}
													if (res.errMsg ==
														"requestPayment:ok") {
														uni.showToast({
															icon: 'none',
															mask: true,
															title: '报名成功',
														});
														setTimeout(() => {
															this.goBack(2)
														}, 1500)
													}
												},
												fail: (err) => {
													console.log(err);
													if (err.errMsg ==
														'requestPayment:fail cancel') {
														uni.showToast({
															icon: 'none',
															mask: true,
															title: '取消支付',
														});
													}
												}
											});
										} else {
											// 余额支付
											uni.showToast({
												icon: 'none',
												mask: true,
												title: '报名成功',
											});
											setTimeout(() => {
												this.goBack(2)
											}, 1500)
										}

									}

								}

							})
						} else {
						
							uni.showToast({
								icon: 'none',
								mask: true,
								title: val.data.msg,
							});
						}
					}
				})
			},
			goBack(index) {
				uni.navigateBack({
					delta: index,
					animationType: 'pop-out',
					animationDuration: 300
				});
			},
		}
	};
</script>

<style scoped lang="scss">
	.pay-method-wrap {
		.page_navbar_warp {
			display: flex;
			align-items: center;
			flex: 1;
			padding: 0 28rpx;

			.navLeft {
				width: 15rpx;
				height: 28rpx;
			}
		}

		.money-box {
			background: #fff;
			height: 250rpx;
			margin-bottom: 20rpx;
			padding-top: 30rpx;

			.time {
				font-size: 28rpx;
				color: #c4c4c4;
			}

			.money {
				color: #e1212b;
				font-size: 60rpx;
				margin-top: 60rpx;

				&::before {
					content: '￥';
					font-size: 46rpx;
				}
			}
		}

		.pay-box {
			.pay-item {
				height: 90rpx;
				padding: 0 30rpx;
				font-size: 26rpx;
				background: #fff;
				width: 750rpx;
				border-bottom: 1rpx solid #eeeeee;

				&:last-child {
					border-bottom: none;
				}

				.pay-img {
					width: 40rpx;
					height: 40rpx;
					margin-right: 25rpx;
				}
			}
		}

		.pay-btn {
			width: 690rpx;
			line-height: 80rpx;
			background: linear-gradient(90deg, rgba(240, 199, 133, 1), rgba(246, 214, 157, 1));
			border-radius: 40rpx;
			color: rgba(#fff, 0.9);
			margin: 100rpx auto 0;
		}
	}
</style>
